/*
 * Copyright (C) 2017, 2019 Samuel Wall
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package uk.co.samuelwall.materialtaptargetprompt.sample;

import ohos.agp.components.BaseItemProvider;
import ohos.agp.components.Component;
import ohos.agp.components.ComponentContainer;
import ohos.agp.components.DependentLayout;
import ohos.agp.components.Image;
import ohos.agp.components.LayoutScatter;
import ohos.agp.components.Text;
import ohos.app.Context;
import uk.co.samuelwall.materialtaptargetprompt.MaterialTapTargetPrompt;

import java.util.ArrayList;

/**
 * CardAdapter
 *
 * @since 2021-06-21
 */
public class CardAdapter extends BaseItemProvider {
    private final Context context;
    private String[] mDataSet = {"foo", "bar", "baz"};
    private ArrayList<Component> list = new ArrayList<>();

    CardAdapter(Context context) {
        this.context = context;
    }

    @Override
    public int getCount() {
        return mDataSet.length;
    }

    @Override
    public Object getItem(int i) {
        return mDataSet[i];
    }

    @Override
    public long getItemId(int id) {
        return id;
    }

    @Override
    public Component getComponent(int i, Component component, ComponentContainer componentContainer) {
        Component cpt;
        BaseViewHolder viewHolder;
        if (component == null) {
            cpt = LayoutScatter.getInstance(componentContainer.getContext())
                    .parse(ResourceTable.Layout_card, null, false);
            viewHolder = new BaseViewHolder(cpt);
            cpt.setTag(viewHolder);
        } else {
            cpt = component;
            viewHolder = (BaseViewHolder) cpt.getTag();
        }
        viewHolder.mText.setText(mDataSet[i]);
        list.add(cpt);
        return cpt;
    }

    /**
     * BaseViewHolder
     *
     * @since 2021-05-15
     */
    static class BaseViewHolder {
        Text mText;
        Image mImageView;
        DependentLayout mDrawLayout;

        BaseViewHolder(Component v) {
            mDrawLayout = (DependentLayout) v.findComponentById(ResourceTable.Id_card_view);
            mText = (Text) v.findComponentById(ResourceTable.Id_info_text);
            mImageView = (Image) v.findComponentById(ResourceTable.Id_info_icon);
        }
    }
}
